package ute.mvc.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import ute.mvc.dao.ClassDAO;
import ute.mvc.dao.Course_SubjectDAO;
import ute.mvc.dao.OpenCourseDAO;
import ute.mvc.dao.SubjectDAO;
import ute.mvc.dao.UserDAO;
import ute.mvc.model.MyClass;
import ute.mvc.model.OpenCourse;
import ute.mvc.model.OpenCourse_Subject;
import ute.mvc.model.Subject;
import ute.mvc.model.User;

@Service("openCourseService")
public class OpenCourseServiceImpl implements OpenCourseService {

	@Autowired
	OpenCourseDAO openCourseDAO;
	@Autowired
	SubjectDAO subjectDAO;
	@Autowired
	Course_SubjectDAO course_SubjectDAO;
	@Autowired
	UserDAO userDAO;
	@Autowired
	ClassDAO classDAO;

	public List<OpenCourse> getAllOpenCourse() {
		return openCourseDAO.getAllOpenCourse();
	}

	public List<OpenCourse> findOpenCourseByID(String openCourseID) {
		return openCourseDAO.findOpenCourseById(openCourseID);
	}

	@Transactional
	public void addOpenCourse(OpenCourse openCourse) {
		openCourseDAO.addOpenCourse(openCourse);
	}

	@Transactional
	public void updateOpenCourse(OpenCourse openCourse) {
		openCourseDAO.updateOpenCourse(openCourse);
	}

	@Transactional
	public void deleteOpenCourse(OpenCourse openCourse) {
		openCourseDAO.deleteOpenCourse(openCourse);
	}

	public OpenCourse getOpenCourseById(String openCourseId) {
		return openCourseDAO.getOpenCourseById(openCourseId);
	}

	public List<Subject> getSubjectByOpenCourseId(String openCourseId) {
		return openCourseDAO.getSubjectByOpenCourseId(openCourseId);
	}

	public List<User> getLecturerByOpenCourseId(String openCourseId) {

		return openCourseDAO.getLecturerByOpenCourseId(openCourseId);
	}

	public OpenCourse getOpenCourseByIdWithListLecturer(String openCourseId) {
		return openCourseDAO.getOpenCourseByIdWithListLecturer(openCourseId);
	}

	public List<Subject> getListAddSubjectByOpenCourseId(String openCourseId) {
		return openCourseDAO.getListAddSubjectByOpenCourseId(openCourseId);
	}

	public void addSubjectToOpenCourse(String openCourseId, String subjectId) {
		OpenCourse openCourse = openCourseDAO.getOpenCourseById(openCourseId);
		Subject subject = subjectDAO.getSubjectById(subjectId);
		OpenCourse_Subject openCourse_Subject = new OpenCourse_Subject();

		openCourse_Subject.setCourse_subjectId(openCourseId + "_" + subjectId);
		openCourse_Subject.setOpenCourse(openCourse);
		openCourse_Subject.setSubject(subject);

		course_SubjectDAO.addCourse_Subject(openCourse_Subject);

	}

	public List<User> getListAddLecturerByOpenCourseId(String openCourseId) {

		return openCourseDAO.getListAddLecturerByOpenCourseId(openCourseId);
	}

	public void addLecturerToOpenCourse(String openCourseId,
			String[] lecturerIds) {
		OpenCourse openCourse = openCourseDAO.getOpenCourseById(openCourseId);
		User lecturer;

		for (String lecturerId : lecturerIds) {
			lecturer = userDAO.getUserById(Integer.parseInt(lecturerId));

			openCourse.getListLecturer().add(lecturer);
		}

		openCourseDAO.updateOpenCourse(openCourse);
	}

	// Vinh
	public List<MyClass> getAllClass(String openCourseId) {
		// TODO Auto-generated method stub
		return openCourseDAO.getAllClass(openCourseId);
	}

	public List<OpenCourse> getOpenCourseByStudent(User student) {
		// TODO Auto-generated method stub
		return openCourseDAO.getOpenCourseByStudent(student);
	}

	
	//Vinh 
	public MyClass getClass(User student, OpenCourse oCourse) {
		// TODO Auto-generated method stub
		return openCourseDAO.getClass(student, oCourse);
	}

	public boolean deleteOpenCourses(String[] openCourseIds) {
		try {
			for(String id : openCourseIds) {
				openCourseDAO.deleteOpenCourse(openCourseDAO.getOpenCourseById(id));
			}
			return true;
		} catch (Exception ex) {
			return false;
		}

	}

	public boolean deleteClass(String[] classIds) {
		try {
			for(String id : classIds) {
				classDAO.deleteClass(classDAO.getClassById(id));
			}
			
			return true;
		} catch(Exception ex) {
			return false;
		}

	}

	public boolean removeSubject(String[] subjectIds, String openCourseId) {
		
		OpenCourse openCourse = openCourseDAO.getOpenCourseById(openCourseId);
		
		try {
			Collection<OpenCourse_Subject> list = openCourse.getListCourse_Subject();
			for(String id: subjectIds) {
				for(OpenCourse_Subject os : list) {
					if(os.getSubject().getSubjectId().equals(id)) {
						course_SubjectDAO.deleteCourse_Subject(os);
					}
				}
			}
			
			return true;
		} catch (Exception ex) {
			return false;
		}
	}

	public boolean removeLecturer(String[] lecturerEmails, String openCourseId) {
		OpenCourse openCourse = openCourseDAO.getOpenCourseById(openCourseId);
		
		try {
			for(String email: lecturerEmails) {
				Collection<User> list = openCourse.getListLecturer();
				for(User u : list) {
					if(u.getEmail().equals(email)) {
						list.remove(u);
						
						openCourseDAO.updateOpenCourse(openCourse);
						break;
					}
				}
			}
			
			return true;
		} catch (Exception ex) {
			ex.printStackTrace();
			return false;
		}
	}
}
